package dryworks.jsf.parsers;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

import dryworks.jsf.examples.generator.GeneratorStringUtils;
import dryworks.jsf.reflect.TagDefinition;
import dryworks.jsf.reflect.TagLib;


public class TLDParser {
	public String getText(Node node) {
		NodeList subList = node.getChildNodes();
		String data = ((Text)subList.item(0)).getData();
		return data;
	}
	
	public String getTextInChild(Node node, String childNodeName) {
		Element element = (Element)node;
		Node childNode = element.getElementsByTagName(childNodeName).item(0);
		NodeList subList = childNode.getChildNodes();
		String data = ((Text)subList.item(0)).getData();
		return data;
	}
	
	public TagLib parse(InputStream stream) throws ParserConfigurationException, SAXException, IOException {
		TagLib res = new TagLib();
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

		DocumentBuilder db;
		dbf.setValidating(false);
		db = dbf.newDocumentBuilder();

		Document document = db.parse(stream);
			
		NodeList toplevel = document.getChildNodes();
		Node first = toplevel.item(0);
		//System.out.println("first.getNodeName()"+first.getNodeName());
		NodeList nodeList = first.getChildNodes();
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node cur = nodeList.item(i);
			
			if (cur instanceof Element) {
				// cur.getAttributes().
				Element curElement = (Element) cur;
				String name = curElement.getNodeName();
				NodeList subList = curElement.getChildNodes();
				String data = ((Text)subList.item(0)).getData();
				//System.out.println("subList:"+subList+":data:"+data);
				//System.out.println(""+name);
				if (name.equals("tlib-version")) {
					res.setTlibVersion(data);
				}
				if (name.equals("jsp-version")) {
					res.setJspVersion(data);
				}
				if (name.equals("short-name")) {
					res.setShortVame(data);
				}
				if (name.equals("uri")) {
					res.setUri(data);
				}
				if (name.equals("description")) {
					res.setDescription(data);
				}
				
				if (name.equals("tag")) {
					TagDefinition tagDefinition = new TagDefinition();
					String tagName = getTextInChild(curElement, "name");
					tagDefinition.setName(tagName);
					//System.out.println(""+tagName);
					String tagClass = getTextInChild(curElement, "tag-class");
					tagDefinition.setTagClass(tagClass);
					
					String bodyContent = getTextInChild(curElement, "body-content");
					tagDefinition.setBodyContent(bodyContent);
					
					// create attributes
					NodeList attributesNodesList = curElement.getElementsByTagName("attribute");
					for (int i1 = 0; i1 < attributesNodesList.getLength(); i1++) {
						Element curAttributeElement = (Element)attributesNodesList.item(i1);
						
						String aName = getTextInChild(curAttributeElement, "name");
						String required = getTextInChild(curAttributeElement, "required");
						String rtexprvalue = getTextInChild(curAttributeElement, "rtexprvalue");
						tagDefinition.addAttribute(aName, required, rtexprvalue);
					}
					res.getTags().put(tagDefinition.getName(), tagDefinition);
				}
			}
		}
		return res;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			TLDParser tldParser = new TLDParser();
			
			File file = new File("inputsForGenerator/icefaces_component.tld");
			
			if (file.exists()) {
				//System.out.println("exist");
			} else {
				System.out.println("not exist");
			}
			
			//tldParser.parse(ParsersTrics.chopFirstDoctype(new FileInputStream(file)));
			TagLib tagLib = tldParser.parse(new FileInputStream(file));
			for (String tagKey: tagLib.getTags().keySet()) {
				System.out.println("CreateExampleTask create"+GeneratorStringUtils.upFirstLetter(tagKey)+" = new CreateExampleTask(\""+tagKey+"\");");
				System.out.println("tasks.add(create"+GeneratorStringUtils.upFirstLetter(tagKey)+");\n");
				//CreateExampleTask createOutputText = new CreateExampleTask("outputText");
				//siteGenerator.getExamplesList().add(createOutputText);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
